package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.dao.entity.QuestionDic;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.QuestionDicRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.QuestionRepositoryService;
import com.ktwlsoft.archivemanage.payload.question.QuestionDicSaveRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionDicUpdateRequest;
import com.ktwlsoft.archivemanage.payload.question.RelatedQuestionRequest;
import com.ktwlsoft.archivemanage.domain.dao.entity.Question;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 问题字典
 *
 * @author Joker
 * @version 1.0
 */
@Component
public class QuestionDicService {

    /**
     * 问题字典
     */
    private final QuestionDicRepositoryService questionDicRepositoryService;
    private final QuestionRepositoryService questionRepositoryService;


    public QuestionDicService(QuestionDicRepositoryService questionDicRepositoryService, QuestionRepositoryService questionRepositoryService) {
        this.questionDicRepositoryService = questionDicRepositoryService;
        this.questionRepositoryService = questionRepositoryService;
    }

    /**
     * 问题字典列表
     *
     * @return Result<?>
     */
    public Result<?> findPage(int pn, int pageSize, String businessFrom) {
        if (businessFrom == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("必须指定使用端");
        }
        PageInfo<QuestionDic> pageInfo = questionDicRepositoryService.findPage(pn, pageSize, businessFrom);
        return Result.ok(pageInfo);
    }

    /**
     * 问题字典新增
     *
     * @return Result<?>
     */
    public Result<?> save(QuestionDicSaveRequest request) {
        QuestionDic questionDic = new QuestionDic();
        BeanUtils.copyProperties(request, questionDic);
        questionDic.setEnable(true);
        try {
            questionDicRepositoryService.insert(questionDic);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR);
        }
        return Result.ok(questionDic.getId());
    }

    /**
     * 问题字典修改
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> edit(QuestionDicUpdateRequest request) {
        QuestionDic questionDic = questionDicRepositoryService.getById(request.getId());
        if (questionDic == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        questionDic.setName(request.getName());
        try {
            questionDicRepositoryService.update(questionDic);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR);
        }
        return Result.ok(questionDic.getId());
    }

    /**
     * 问题字典启用禁用
     *
     * @param id id
     * @return Result<?>
     */
    public Result<?> enableById(Long id) {
        if (id == null || id <= 0) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        QuestionDic questionDic = questionDicRepositoryService.getById(id);
        if (questionDic == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        questionDic.setEnable(!questionDic.getEnable());
        try {
            questionDicRepositoryService.update(questionDic);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR);
        }
        return Result.ok(questionDic.getName());
    }

    /**
     * 问题字典删除
     *
     * @param id id
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteById(Long id) {
        if (id == null || id <= 0) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        QuestionDic questionDic = questionDicRepositoryService.getById(id);
        if (questionDic == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        if (questionDic.getId() != null) {
            //是否有引用
            RelatedQuestionRequest relatedQuestionRequest = new RelatedQuestionRequest();
            relatedQuestionRequest.setId(id);
            List<Question> questions = questionRepositoryService.getQuestionMapper().getByCategoryId(relatedQuestionRequest);
            if (questions != null && questions.size() > 0) {
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("无法删除,该数据已被引用");
            }
        }
        try {
            questionDicRepositoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.DELETE_ERROR);
        }
        return Result.ok(questionDic.getId());
    }

    /**
     * 获取问题导航下的问题列表
     *
     * @param request
     * @return
     */
    public Result<?> getByCategoryId(RelatedQuestionRequest request) {
        PageInfo<Question> pageInfo = questionDicRepositoryService.getByCategoryId(request);
        return Result.ok(pageInfo);
    }

    /**
     * 处理value
     *
     * @param questionDic
     * @return 字段存在true, 不存在false
     */
    private boolean handelValue(QuestionDic questionDic) {
        if (questionDic.getName() == null || questionDic.getName() == null || "".equals(questionDic.getName())) {
            return false;
        }
        List<QuestionDic> questionDics = questionDicRepositoryService.getByName(questionDic);
        if (questionDics != null && questionDics.size() > 0) {
            for (QuestionDic dic : questionDics) {
                if (!dic.getId().equals(questionDic.getId())) {
                    return true;
                }
            }
        }
        return false;
    }
}